{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周主要内容：图像识别（人脸识别）\n",
    "## 23春_API_人工智能与机器学习_week04\n",
    "本周内容\n",
    "1. 人脸识别概念和基础\n",
    "2. 人脸识别应用\n",
    "3. 人脸识别实践-face++\n",
    ">1.人脸检测\n",
    ">2.人脸比对+人脸搜索\n",
    ">>3.人脸集合创建\n",
    ">>4.人脸"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 人脸识别概念和基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679661651,14310ec6-80a4-4de9-89af-7e8e3dd938d0', 'time_used': 310, 'faces': [{'face_token': '0aff8055d91ce37e27ecde87484f3d20', 'face_rectangle': {'top': 403, 'left': 292, 'width': 293, 'height': 293}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 23}, 'smile': {'value': 0.408, 'threshold': 50.0}, 'headpose': {'pitch_angle': 5.5033426, 'roll_angle': -4.3039246, 'yaw_angle': 5.2246637}, 'blur': {'blurness': {'value': 0.447, 'threshold': 50.0}, 'motionblur': {'value': 0.447, 'threshold': 50.0}, 'gaussianblur': {'value': 0.447, 'threshold': 50.0}}, 'emotion': {'anger': 0.026, 'disgust': 0.317, 'fear': 0.026, 'happiness': 36.089, 'neutral': 3.741, 'sadness': 0.038, 'surprise': 59.763}, 'facequality': {'value': 89.56, 'threshold': 70.1}, 'beauty': {'male_score': 83.001, 'female_score': 85.997}}}], 'image_id': '+3t/bLHqm5xm8zpOlBvCKw==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "#二进制方式打开图片文件\n",
    "#1.图片文件准备\n",
    "f = open(r'C:\\bailu.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload = {\n",
    "    'api_key':'V2gypDr1_IwCuMn9qzk7qwUpoosqEnq0',\n",
    "    'api_secret':'mO8madChmDyx_GDQwUsymItYdP13qzcr',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,emotion,blur,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data = payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679798230,36fac857-fc77-49b6-a6f0-f7f29f8da7dd', 'time_used': 160, 'faces': [{'face_token': '92cda1170fbfc0963332e5b32130d804', 'face_rectangle': {'top': 195, 'left': 173, 'width': 103, 'height': 103}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 22}, 'smile': {'value': 0.023, 'threshold': 50.0}, 'headpose': {'pitch_angle': 9.956847, 'roll_angle': -0.81994003, 'yaw_angle': 4.292511}, 'blur': {'blurness': {'value': 0.359, 'threshold': 50.0}, 'motionblur': {'value': 0.359, 'threshold': 50.0}, 'gaussianblur': {'value': 0.359, 'threshold': 50.0}}, 'emotion': {'anger': 0.05, 'disgust': 0.05, 'fear': 0.056, 'happiness': 0.187, 'neutral': 23.287, 'sadness': 0.1, 'surprise': 76.271}, 'facequality': {'value': 88.265, 'threshold': 70.1}, 'beauty': {'male_score': 87.811, 'female_score': 90.436}}}], 'image_id': 'mPvin3Q2D7oHIx4S7F4ZIA==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "#二进制方式打开图片文件\n",
    "#1.图片文件准备\n",
    "f = open(r'C:\\pxr.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload = {\n",
    "    'api_key':'V2gypDr1_IwCuMn9qzk7qwUpoosqEnq0',\n",
    "    'api_secret':'mO8madChmDyx_GDQwUsymItYdP13qzcr',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,emotion,blur,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data = payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679798337,ff61490b-d38f-473b-b55d-6596bbe226a5', 'time_used': 474, 'faces': [{'face_token': '84415d769921bbb9eb5d8889d15321b3', 'face_rectangle': {'top': 176, 'left': 290, 'width': 220, 'height': 220}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 20}, 'smile': {'value': 99.999, 'threshold': 50.0}, 'headpose': {'pitch_angle': 0.47800052, 'roll_angle': 7.951721, 'yaw_angle': 21.038551}, 'blur': {'blurness': {'value': 0.03, 'threshold': 50.0}, 'motionblur': {'value': 0.03, 'threshold': 50.0}, 'gaussianblur': {'value': 0.03, 'threshold': 50.0}}, 'emotion': {'anger': 0.0, 'disgust': 0.0, 'fear': 0.0, 'happiness': 100.0, 'neutral': 0.0, 'sadness': 0.0, 'surprise': 0.0}, 'facequality': {'value': 13.728, 'threshold': 70.1}, 'beauty': {'male_score': 81.03, 'female_score': 83.451}}}], 'image_id': 'PRQz2j23TCYtUvsgrXBVJw==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "#二进制方式打开图片文件\n",
    "#1.图片文件准备\n",
    "f = open(r'C:\\nini.jfif', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload = {\n",
    "    'api_key':'V2gypDr1_IwCuMn9qzk7qwUpoosqEnq0',\n",
    "    'api_secret':'mO8madChmDyx_GDQwUsymItYdP13qzcr',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,emotion,blur,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data = payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
